﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using NPOI.SS.Formula.Functions;
using System.Text.RegularExpressions;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace KD.Sms.UI_Query
{
    public partial class frmImage : Form
    {
        private Bitmap map;
        string drawImg = "";
        string _samplingid;
        DataTable _tabImage;
        string carNo;
        public frmImage()
        {
            InitializeComponent();
        }

        public frmImage(string samplingid)
        {
            InitializeComponent();
            _samplingid = samplingid;
        }

        private void frmImage_Load(object sender, EventArgs e)
        {
            _tabImage = DAL.SqlLite.Main.Instance.DoQueryImgesBySamplingId(_samplingid);
            if (_tabImage != null && _tabImage.Rows.Count > 0)
            {
                DoShowImage(picBefore, _tabImage.Rows[0]["Img_Begin"].ToString());
                DoShowImage(picAfter, _tabImage.Rows[0]["Img_End"].ToString());
                //读取画框图
                drawImg = GetImageFilePath(_tabImage.Rows[0]["Img_Begin"].ToString());
                DoShowImage(picDraw, drawImg);
            }
        }

        public string GetImageFilePath(string oriPath)
        {
            try
            {
                DataTable sampData = DAL.SqlLite.Main.Instance.DoQueryCarNoBySamplingId(_samplingid);
                if (sampData != null)
                {
                    //提取纯数字，该方式会将所有数字提取出来并拼接在一起，如："ABC#123@AS456测试"提取出来就是123456
                    var dateDir = Regex.Replace(oriPath, @"[^0-9]+", "").Substring(0, 6);

                    carNo = sampData.Rows[0]["Car_NO"].ToStringEx();
                    DateTime Sampling_Time = sampData.Rows[0]["Sampling_Time"].ToDateTimeEx(DateTime.Now);
                    string E_Root = $"E:\\CyImage\\{dateDir}\\";
                    var files = Directory.GetFiles(E_Root);
                    var listFile = files.ToList<string>();
                    var imagePath = listFile.Where(m => m.Contains(carNo)).ToList();
                    if (imagePath.Count == 1)
                        return imagePath[0];
                    string path = "";
                    foreach (var res in imagePath)
                    {
                        System.IO.FileInfo fileInfo = new System.IO.FileInfo(res);
                        if (fileInfo.CreationTime <= Sampling_Time.AddHours(1))
                            if (fileInfo.CreationTime >= Sampling_Time.AddHours(-1))
                            {
                                path = res;
                                break;
                            }
                    };
                    return path;
                }
                return "";
            }
            catch
            {
                return "";
            }
        }

        private void frmImage_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape) this.Close();
        }

        void DoShowImage(PictureBox p, string path)
        {
            try
            {
                map = new Bitmap(path);
                if (map == null) p.Visible = false;
                p.BackgroundImage = map;
            }
            catch
            {
                p.Visible = false;
            }
        }

        private void picBefore_DoubleClick(object sender, EventArgs e)
        {
            frmPreview frmPreview = new frmPreview(_tabImage.Rows[0]["Img_Begin"].ToString());
            frmPreview.ShowDialog();
        }

        private void picAfter_DoubleClick(object sender, EventArgs e)
        {
            frmPreview frmPreview = new frmPreview(_tabImage.Rows[0]["Img_End"].ToString());
            frmPreview.ShowDialog();
        }

        private void picDraw_DoubleClick(object sender, EventArgs e)
        {
            frmPreview frmPreview = new frmPreview(drawImg);
            frmPreview.ShowDialog();
            
        }
    }
}
